package uiLayer;

import modelLayer.OrderLine;
import uiUtil.AbstractEditableCustomTable;

public class EditableOrderLineTable extends AbstractEditableCustomTable<OrderLine> {
	
	public EditableOrderLineTable() {
		
		super();
		super.setColNames("Varenr.", "Navn", "Pris", "Antal", "Tilføj/Fjern");
		//Property on JTable which stops editing and saves changes when focus lost from the table:
		putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); 

	}

	@Override
	public boolean doIsCellEditable(int rowIndex, int columnIndex) {
		
		if (columnIndex < 3) {
			return false;
		}
		else {
			return true;
		}
		
	}

	@Override
	public void doSetValueAt(Object aValue, int col, OrderLine ol) {
		
		System.out.println("doSetValueAt() " + aValue + ", " + col);
		switch (col) {
			case 3:
				ol.setAmount((Integer)aValue);
				break;
			case 4:
				//ol.setAdded((Boolean)aValue);
				break;
		}
		
	}

	@Override
	public Object getCellValue(int col, OrderLine ol) {
		
		Object res = null;
		switch (col) {
			case 0:
				res = ol.getProduct().getProductID();
				break;
			case 1:
				res = ol.getProduct().getName();
				break;
			case 2:
				res = ol.getProduct().getSalesPrice();
				break;
			case 3:
				res = ol.getAmount();
				break;
			case 4:
				//res = ol.isAdded();
				break;
		}
		return res;
		
	}

}
